package com.centerm.structure.service.impl;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centerm.common.exception.MyRuntimeException;
import com.centerm.structure.dao.RolePermissionDao;
import com.centerm.structure.domain.RolePermission;
import com.centerm.structure.dto.PermissionInfo;
import com.centerm.structure.dto.RolePermissionInfo;
import com.centerm.structure.service.RolePermissionService;

/**
 * 
* @ClassName: RolePermissionServiceImpl
* @Description: 角色权限关系实现类
* @author czy
* @date 2017年8月16日
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    private RolePermissionDao rolePermissionDao;

	@Override
	@Transactional
	public Integer update(RolePermissionInfo rolePermissionInfo) {
		Integer result = 0;
		String ids = rolePermissionInfo.getPermissionIds();
		Integer roleId = rolePermissionInfo.getRoleId();
		if(StringUtils.isBlank(ids)){
			throw new MyRuntimeException("权限id集合permissionIds can not be null!");
		}
		//先删除旧记录
		rolePermissionDao.deleteByRoleId(roleId);
		//再新增新记录
		RolePermission rolePermission = null;
		for(String id : ids.split(",")){
			rolePermission = new RolePermission();
			rolePermission.setRoleId(roleId);
			rolePermission.setPermissionId(Integer.valueOf(id));
			rolePermissionDao.insert(rolePermission);
			result++;
		}
		return result;
	}

	@Override
	public List<PermissionInfo> listByParam(
			RolePermissionInfo rolePermissionInfo) {
	    List<PermissionInfo> permissionInfoList = rolePermissionDao.listByRoleId(rolePermissionInfo);
		return permissionInfoList;
	}

}
